package com.zhengke.service.ddgl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhengke.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.Result;

/**
 * *******************************
 * 创建时间：2019年12月26日 下午3:05:08
 * 项目名称：tvmallbs Maven Webapp
 *
 * @author 王鑫
 * @version 1.0
 * 文件名称：DDGL060ServiceImpl.java
 * 类说明：
 * <p>
 * *******************************
 */
@Service
public class DDGL060ServiceImpl implements DDGL060Service {

    @Autowired
    private CommonDao dao;


    @Override
    public Result queryOrder(String orderSn, String userName,String userCardNumber,String receiverPhone, String orderState,int pageNum, int size) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("orderSn", orderSn);
        param.put("userName", userName);
        param.put("userCardNumber", userCardNumber);
        param.put("receiverPhone", receiverPhone);
        param.put("orderState", orderState);

        PageHelper.startPage(pageNum, size, true);
        List<RtnGoodsMsg> rtnList = (List<RtnGoodsMsg>) dao.findForList("ddgl060Mapper.queryRtOrder", param);
        PageInfo<RtnGoodsMsg> pageInfo = new PageInfo<>(rtnList);

        Result rs = new Result();
        rs.setCode("0");
        rs.setCount(pageInfo.getTotal());
        rs.setData(rtnList);
        return rs;

    }

    @Override
    public Result rtOrderDetail(String orderId) throws Exception {
        List<TvOrderGoods> rtnList = (List<TvOrderGoods>) dao.findForList("ddgl060Mapper.queryRtDetail",  Integer.parseInt(orderId));
        for (TvOrderGoods tog:rtnList) {
            Integer paramValue01 = Integer.parseInt(tog.getRtItem01().split(":")[0]);//兑换比例
            Double paramValue02 = Double.parseDouble(tog.getRtItem02());//退货费用1
            String deployInfo = tog.getDeployInfo();
            if (deployInfo == null || "".equals(deployInfo)){
                deployInfo = String.valueOf(paramValue01*paramValue02);
                tog.setDeployInfo(deployInfo);
            }
        }
        PageInfo<TvOrderGoods> tvOrderGoodsPageInfo = new PageInfo<>();
        Result rs = new Result();
        rs.setCode("200");
        rs.setMsg("");
        rs.setCount(tvOrderGoodsPageInfo.getTotal());
        rs.setData(rtnList);
        return rs;
    }

    /**
     * 查询一个TvOrderGoods对象
     * @param recordId
     * @return
     */
    @Override
    public TvOrderGoods queryTogByRecordId(String recordId) throws Exception {
        TvOrderGoods tog = (TvOrderGoods) dao.findForObject("ddgl060Mapper.queryTogByRecordId", Integer.parseInt(recordId));
        return tog;
    }

    /**
     * 完成退货，更新信息
     * @param tog
     * @param to
     * @param tu
     * @param msg
     * @param til
     */
    @Override
    public void toReturn(TvOrderGoods tog, TvOrder to, TvUser tu, MallServiceGoods01 msg, TvUserIntegralLog til) throws Exception {
        dao.update("ddgl060Mapper.updateByPrimaryKey",tog);
        dao.update("orderMapper.updateByPrimaryKey",to);
        dao.update("jfdhMapper03.updateByPrimaryKey",tu);
        dao.update("jfdhMapper02.updateByPrimaryKey",msg);
        dao.save("tvUserIntegralLogMapper.insertSelective",til);
    }
}
